home *** CD-ROM | disk | FTP | other *** search
- Path: news.clark.net!usenet
- From: gusty@clark.net (Harlan Messinger)
- Newsgroups: comp.lang.c++
- Subject: Re: Newbie question on syntax of pointer to const
- Date: Tue, 30 Jan 1996 03:43:09 GMT
- Organization: Clark Internet Services, Inc.
- Message-ID: <4ek468$jr1@clarknet.clark.net>
- References: <4ej9eg$lq6@agate.berkeley.edu>
- NNTP-Posting-Host: gusty-ppp.clark.net
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
- X-Newsreader: Forte Free Agent 1.0.82
-
- parsons@vouvray.CS.Berkeley.EDU (David C. Parsons) wrote:
-
- >In a declaration of a const reference or const pointer type, does it matter
- >in what the order the keyword const and the underlying type appear? That
- >is, are
-
- >(1) const double *pc; and
- >(2) double const *pc;
-
- >both acceptable syntax for type "pointer to constant double"? (My book only
- >describes (1), but my compiler accepts either.)
-
- This is one of my favorite questions to ask C/C++ programmers at job
- interviews.
-
- They are different. The first is a pointer to a constant double.
- Whatever pc points to is treated as a constant when referenced via pc.
- That is, it prevents you from making assignments like
-
- *pc = 3.14;
-
- The second is a constant pointer to a double. You can change the value
- of the double that pc points to all you want, but the pointer itself
- can only ever point to one place in memory, and you have to specify
- where that is at declaration time. Just as you can't have
-
- const double threepointtwo;
- threepointtwo = 3.2;
-
- but need
-
- const double threepointtwo = 3.2;
-
- you likewise have to have something like
-
- double arrayOfDoubles[25];
- double const pc = arrayOfDoubles;
-
- or
-
- double threepointtwo = 3.2;
- double const pc = &threepointtwo;
-
- You can even have const double const pc, a constant pointer to a
- constant double. I'm not sure what good it is.
-
-
-